import 'package:enjoy_plus/smart_shop/pages/cart/index.dart';
import 'package:enjoy_plus/smart_shop/pages/category/index.dart';
import 'package:enjoy_plus/smart_shop/pages/home/index.dart';
import 'package:enjoy_plus/smart_shop/pages/mine/index.dart';
import 'package:flutter/material.dart';

class TabBarPage extends StatefulWidget {
  const TabBarPage({super.key});

  @override
  State<TabBarPage> createState() => _TabBarPageState();
}

//底部导航栏item
List<BottomNavigationBarItem> barItemList() {
  List<BottomNavigationBarItem> items = [];
  //添加首页
  items.add(
    BottomNavigationBarItem(
        icon: Image.asset(
          'tabbar/home.png',
          width: 32,
          height: 32,
        ),
        activeIcon: Image.asset(
          'tabbar/home-active.png',
          width: 32,
          height: 32,
        ),
        label: '首页'),
  );
  //添加分类页
  items.add(
    BottomNavigationBarItem(
        icon: Image.asset(
          'tabbar/cate.png',
          width: 32,
          height: 32,
        ),
        activeIcon: Image.asset(
          'tabbar/cate-active.png',
          width: 32,
          height: 32,
        ),
        label: '分类'),
  );
  //添加购物车页
  items.add(
    BottomNavigationBarItem(
        icon: Image.asset(
          'tabbar/cart.png',
          width: 32,
          height: 32,
        ),
        activeIcon: Image.asset(
          'tabbar/cart-active.png',
          width: 32,
          height: 32,
        ),
        label: '购物车'),
  );
  //添加我的页
  items.add(
    BottomNavigationBarItem(
        icon: Image.asset(
          'tabbar/user.png',
          width: 32,
          height: 32,
        ),
        activeIcon: Image.asset(
          'tabbar/user-active.png',
          width: 32,
          height: 32,
        ),
        label: '我的'),
  );
  return items;
}

class _TabBarPageState extends State<TabBarPage> {
  //记录当前索引
  int currentIndex = 0;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      //安全区
      body: SafeArea(
        child: IndexedStack(
          index: currentIndex,
          children: const [HomePage(), CategoryPage(), CartPage(), MinePage()],
        ),
      ),
      //底部导航栏
      bottomNavigationBar: BottomNavigationBar(
        type: BottomNavigationBarType.fixed, //固定底部导航栏
        items: barItemList(), //底部导航栏item
        currentIndex: currentIndex, //当前索引
        selectedItemColor: Colors.red, //选中项颜色
        unselectedItemColor: Colors.black, //未选中项颜色
        onTap: (index) {
          //点击切换索引
          setState(() {
            currentIndex = index;
          });
        },
      ),
    );
  }
}
